package controller

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"go_test/src/code.song.study/web/dao"
	"go_test/src/code.song.study/web/model"
	"strconv"
)

type VideoController struct {
	beego.Controller
}

// 查询视频列表
func (v *VideoController) QueryVideoList() {
	videoReq := model.VideoReqBO{}
	err := json.Unmarshal(v.Ctx.Input.RequestBody, &videoReq)
	if err != nil {
		result := model.Build("A001", "参数异常", nil)
		v.Data["json"] = result
		v.ServeJSON()
	}

	count := dao.GetVideoInfoCount(videoReq)
	if count == 0 {
		result := model.BuildPageResult(count, nil)
		v.Data["json"] = result
		v.ServeJSON()
	}

	videoList := dao.GetAllVideoInfoList(videoReq)
	result := model.BuildPageResult(count, videoList)
	BuildBaseJson(result, v)
}

// 查询视频详情
func (v *VideoController) GetVideoInfoDetail() {
	videoId := v.Ctx.Input.Param(":id")
	id, _ := strconv.Atoi(videoId)
	videoInfo := dao.GetVideoDetailInfo(id)
	videoInfoResult := model.BuildSuccessWithData(videoInfo)
	BuildBaseJson(videoInfoResult, v)
}

func WebStatus(context *context.Context) {
	result := model.BuildSuccessWithNoParam()
	_ = context.Output.JSON(result, true, false)
}

func BuildBaseJson(data interface{}, v *VideoController) {
	v.Data["json"] = data
	v.ServeJSON()
}
